#!/usr/bin/env python
# -*- coding:utf-8 -*-


class MemberPointLevelObj:
    MEMBER_DEFAULT_LEVEL = "A"

    MEM_POINT_LEVEL = [
        {"max_points": 50, "key": "A"},
        {"max_points": 200, "key": "B"},
        {"max_points": 600, "key": "C"},
        {"max_points": 1000, "key": "D"},
    ]

    MEM_LEVEL = {
        "A": "u普通会员",
        "B": "u高级会员",
        "C": "u黄金会员",
        "D": "u钻石会员",
    }
    col_keys = ["points", "level"]
    col_def_val = [float(0), ""]

    points: float(0)
    level: ""

    def __init__(self):
        return

    @staticmethod
    def make_reg_data(data):
        for item_index in range(len(MemberPointLevelObj.col_keys)):
            try:
                _check_val = data[str(MemberPointLevelObj.col_keys[item_index]).lower()]
                if (isinstance(_check_val, int) or isinstance(_check_val, float)):
                    if _check_val is None:
                        _val = MemberPointLevelObj.col_def_val[item_index]
                    else:
                        if isinstance(_check_val, int):
                            _val = int(_check_val)
                        elif isinstance(_check_val, float):
                            _val = float(_check_val)
                        else:
                            _val = round(_check_val, 2)
                else:
                    if _check_val is not None and len(_check_val) > 0:
                        _val = data[str(MemberPointLevelObj.col_keys[item_index]).lower()]  # 这里获取页面数值
                    else:
                        _val = MemberPointLevelObj.col_def_val[item_index]  # 这里配置数据库默认值
            except KeyError as e:
                _val = ''
            data[str(MemberPointLevelObj.col_keys[item_index]).upper()] = _val

    @staticmethod
    def split_params(params):
        import math
        g_param = []
        if params['points'] is not None and math.fabs(int(params['points'])) > 0:
            g_param.append('`points`=' + "\'" + str(params['points']) + "\'")
        if params['level'] is not None and len(params['level']) > 0:
            g_param.append('`level`=' + "\'" + params['level'] + "\'")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def concat_datas(g_param):
        arry = [g_param["points"], g_param["level"]]
        _res = []
        for _val in arry:
            if isinstance(_val, str):
                _res.append(f"\'{_val}\'")
            else:
                if _val is None:
                    _res.append("null")
                else:
                    _res.append(_val)
        return ', '.join(str(i) for i in _res)

    @staticmethod
    def load_db_2_dict(data):
        g_param = {}
        g_param["id"] = data[0]
        g_param["points"] = data[1]
        g_param["level"] = data[2]
        g_param["key"] = data[3]
        return g_param

    # 标记为true时候，从DBA中读取数据，附带ID主键，标记为false时候忽略自增型字段ID
    @staticmethod
    def to_string(with_flg):
        g_param = []
        if with_flg:
            g_param.append("`mem_member_point_level`.`id`")
            g_param.append("`mem_member_point_level`.`points`")
            g_param.append("`mem_member_point_level`.`level`")
            g_param.append("`mem_member_point_level`.`key`")
        else:
            g_param.append("`points`")
            g_param.append("`level`")
            g_param.append("`key`")
        return ', '.join(str(i) for i in g_param)
